package com.example.yls.demoa;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button insertStu,queryStu,delStu,updateStu;
    private TextView listText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        insertStu = findViewById(R.id.insert_student);
        queryStu = findViewById(R.id.query_student);
        delStu = findViewById(R.id.del_student);
        updateStu = findViewById(R.id.update_student);
        listText = findViewById(R.id.list_text);

        insertStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                // 插入表中数据
                ContentValues values = new ContentValues();
                values.put("stuno", 1);
                values.put("name", "小二");
                values.put("age", 18);
                getContentResolver().insert(uri,values);
            }
        });

        queryStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");

                Cursor cursor = getContentResolver()
                        .query(uri,null,null,
                                null,null);

                if(cursor != null && cursor.getCount()>0){
                    StringBuilder sb = new StringBuilder();
                    for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
                        sb.append("学号："+cursor.getString(0));
                        sb.append(" 姓名："+cursor.getString(1));
                        sb.append(" 年龄："+cursor.getString(2));
                        sb.append("\n");
                    }
                    listText.setText(sb.toString());
                }else{
                    listText.setText("无数据");
                }

            }
        });

        delStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");

                int i = getContentResolver().delete(uri,null,null);

                Toast.makeText(MainActivity.this,
                        "已删除学生"+i+"条",Toast.LENGTH_LONG).show();

            }
        });

        updateStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");

                ContentValues values = new ContentValues();
                values.put("stuno", 2);
                values.put("name", "小三");
                values.put("age", 19);

                int i = getContentResolver().update(uri,values,null,null);

                Toast.makeText(MainActivity.this,
                        "已更新学生"+i+"条",Toast.LENGTH_LONG).show();

            }
        });

    }
}
